﻿interface Number {
    /**
     * 从左补齐字符串宽度
     * @param {number} length 目标字符串宽度
     * @param {string} padStr 用来补齐的字符，默认为空格
     */
    PadLeft(length: number, padStr: string): string;
    /**
     * 从右补齐字符串宽度
     * @param {number} length 目标字符串宽度
     * @param {string} padStr 用来补齐的字符，默认为空格
     */
    PadRight(length: number, padStr: string): string;

}

module NumberHelper {
    Number.prototype.PadLeft = function (a, b) {
        return String(this).PadLeft(a, b);
    };
    Number.prototype.PadRight = function (a, b) {
        return String(this).PadRight(a, b);
    };
}




interface String {
    /**
     * 从左补齐字符串宽度
     * @param {number} length 目标字符串宽度
     * @param {string} padStr 用来补齐的字符，默认为空格
     */
    PadLeft(length: number, padStr: string): string;
    /**
     * 从右补齐字符串宽度
     * @param {number} length 目标字符串宽度
     * @param {string} padStr 用来补齐的字符，默认为空格
     */
    PadRight(length: number, padStr: string): string;
    /**
     * 格式化字符串，用法同c#
     * @param {type} ...args 
     */
    format(...args: any[]): string; 
    /**
     * 字符串转整数
     */
    ToInt(): Number;
    /**
     * 字符串转浮点
     */
    ToFloat(): Number;
    /**
     * 移除字符串中的html标签
     */
    RemoveHtmlTag(): string;
    /**
     * 根据字符数截断文字(汉字算2个字符),如果原字符串超长自动追加……
     * @param {type} len
     */
    SubStringByByte(len: number): string
}



module StringHelper {
    /**
     * 获取待补齐的字符
     * @param {string} src
     * @param {number} length
     * @param {string} padStr
     * @returns
     */
    function PadHelper(src: string, length: number, padStr: string) {
        if (!padStr) padStr = " ";
        if (src.length < length) {
            var d = new String();
            for (var i = 1; i <= length - src.length; i++) d += padStr;
            return d;
        }
        return "";
    };

    String.prototype.SubStringByByte = function (n: number): string {
        var str = this;
        if (!str || !n) return "";
        var a = 0;
        var i = 0;
        var temp = "";
        for (i = 0; i < str.length; i++) {
            a += str.charCodeAt(i) > 255 ? 2 : 1;
            if (a > n) {
                return temp + "...";
            }
            temp += str.charAt(i);
        }
        return str;
    };

    String.prototype.PadLeft = function (length, str) {
        return PadHelper(this, length, str) + this;
    };

    String.prototype.PadRight = function (length, str) {
        return this + PadHelper(this, length, str);
    };

    String.prototype.format = function (...args: any[]) {
        var a = arguments;
        return 0 === arguments.length ? this : this.replace(/\{(\d+)\}/g,
            (b: any, c: any) => a[parseInt(c)]);
    };

    String.prototype.ToInt = function () {
        if (!this) return 0;
        var a = parseInt(this);
        return isNaN(a) ? 0 : a;
    };

    String.prototype.ToFloat = function () {
        if (!this) return 0;
        var a = parseFloat(this);
        return isNaN(a) ?0:a;
    };

    String.prototype.RemoveHtmlTag = function () {
        var str = this;
        str = str.replace(/<\/?[^>]*>/g, "");
        str = str.replace(/[ | ]*\n/g, "\n");
        str = str.replace(/&nbsp;/gi, "");
        return str;
    };

}



